﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_CommonNewsEdit : System.Web.UI.Page
{
    public int News_id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["admin"] == null)
        {
            Response.Redirect("../Default.aspx");
        }
        else
        {

            List.Text = Session["List"].ToString();
            News_Edit.Text = Session["NewsType"].ToString();
            if (Session["OperationType"].ToString().Equals("New"))
            {

            }
            else if (Session["OperationType"].ToString().Equals("Edit"))
            {
                News_id = Convert.ToInt32(Session["News_id"]);
                NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
                var News = from news in dc.News
                           where news.News_id.Equals(News_id)
                           select new
                           {
                               标题 = news.Title,
                               内容 = news.Content
                           };

                foreach (var news in News)
                {
                    content.Value = Convert.ToString(news.内容);
                    title.Value = Convert.ToString(news.标题);
                }
                dc.Dispose();
            }
        }
    }
    protected void Logoff_Click(object sender, EventArgs e)
    {
        Session.Remove("admin");
        Response.Redirect("../Default.aspx");
    }

    protected void MenuLogoff_Click(object sender, EventArgs e)
    {
        Session.Remove("admin");
        Response.Redirect("../Default.aspx");
    }
    protected void PictureNewsManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "图片新闻编辑");
        Response.Redirect("AdminHome.aspx");
    }
    protected void CollegeNewsManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "学院新闻编辑");
        Response.Redirect("AdminHome.aspx");
    }
    protected void NoticeManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "信息公告编辑");
        Response.Redirect("AdminHome.aspx");
    }
    protected void IndependentInstituteDynamicManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "独立学院动态编辑");
        Response.Redirect("AdminHome.aspx");
    }
    protected void CampusDynamicManagement_Click(object sender, EventArgs e)
    {
        Session.Add("InitType", "校园动态编辑");
        Response.Redirect("AdminHome.aspx");
    }
    protected void ChangePassword_Click(object sender, EventArgs e)
    {
        Response.Redirect("ChangePassword.aspx");
    }
    protected void Submit_Button_Click(object sender, EventArgs e)
    {
        if (Session["OperationType"].ToString().Equals("New"))
        {
            string NewsType = Session["NewsType"].ToString().Replace("编辑", "");
            int catalog_id = 0;
            int news_id = 0;
            
            NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
            var Catalog = from catalog in dc.Catalogs
                           where catalog.CatalogName.Equals(NewsType)
                           select catalog;
            foreach(var catalog in Catalog)
            {
                catalog_id = catalog.Catalog_id;
            }


            News news = new News();
            news.Time = DateTime.Now;
            news.Title = Request.Form["title"];
            news.Content = Request.Form["content"];
            
            try
            {
                SQL.SQLHelper.Insert(news);
            }
            catch(Exception ex)
            {
            }

            var News = from pNews in dc.News
                       where pNews.Title.Equals(Request.Form["title"].ToString())
                       select pNews;
            foreach (var pNews in News)
            {
                news_id = pNews.News_id;
            }
            dc.Dispose();

            NewsToCatalog newsToCatalog = new NewsToCatalog();
            newsToCatalog.News_id = news_id;
            newsToCatalog.Catalog_id = catalog_id;

            try 
            {
                SQL.SQLHelper.Insert(newsToCatalog);
            }
            catch (Exception ex)
            {
            }

            title.Value = Request.Form["title"];
            content.Value = Request.Form["content"];
        }
        else if (Session["OperationType"].ToString().Equals("Edit"))
        {
            NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
            var News = from news in dc.News
                       where news.News_id.Equals(News_id)
                       select news;
            foreach (var news in News)
            {
                news.Title = Request.Form["title"];
                news.Content = Request.Form["content"];
            }
            title.Value = Request.Form["title"];
            content.Value = Request.Form["content"];
            dc.SubmitChanges();
            dc.Dispose();
            
        }
        
    }
    protected void Return_Button_Click(object sender, EventArgs e)
    {
        this.RegisterClientScriptBlock("E", "<script language=javascript>history.go(-2);</script>");
    }
}
